// $codepro.audit.disable com.instantiations.assist.eclipse.analysis.audit.rule.effectivejava.alwaysOverridetoString.alwaysOverrideToString, unnecessaryExceptions
/*
 * PersistenceManager.java 
 */
package edu.gatech.oad.persistence;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * @author cwakamo
 * @version 1.0
 */
public class PersistenceManager {
	
	public static void save(PersistentObject object, String filePath)
	throws FileNotFoundException {
		ObjectOutputStream out = null;
		try {
			out = new ObjectOutputStream(new FileOutputStream(filePath));
			out.writeObject(object);
		}
		catch (FileNotFoundException e) {
			throw e;
		}
		catch (IOException e) {
			e.printStackTrace();
		}
		finally {
			try {
				if (out != null) {
					out.close();
				}
			}
			catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	public static PersistentObject load(String filePath) 
	throws FileNotFoundException {
		PersistentObject obj = null;
		ObjectInputStream in = null;
		try {
			in = new ObjectInputStream(new FileInputStream(filePath));
			obj = (PersistentObject) in.readObject();
		}
		catch (FileNotFoundException e) {
			throw e;
		}
		catch (IOException e) {
			e.printStackTrace();
		}
		catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		finally {
			if (in != null) {
				try {
					in.close();
				} 
				catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return obj;
	}
	
}
